package cn.kmust.booth.portal.service.impl;

import cn.kmust.booth.portal.mapper.TypeCostMapper;
import cn.kmust.booth.portal.model.TypeCost;
import cn.kmust.booth.portal.service.ITypeCostService;
import cn.kmust.booth.portal.service.ex.DeleteException;
import cn.kmust.booth.portal.service.ex.InsertException;
import cn.kmust.booth.portal.service.ex.StallNotFoundException;
import cn.kmust.booth.portal.service.ex.UpdateException;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhYoG
 * @since 2020-08-05
 */
@Service
public class TypeCostServiceImpl extends ServiceImpl<TypeCostMapper, TypeCost> implements ITypeCostService {
    @Autowired
    private TypeCostMapper typeCostMapper;

    @Override
    public TypeCost add(TypeCost typeCost, Integer userId) {
        //判断用户id是否是管理员
//        User user = UserMapper.selectById(userId);
//        if(user == null){
//            throw new PermissionDeniedException("您不具备添加类型费用的权限!请登录再试");
//        }

        //创建TypeCost对象
        //补全TypeCost对象的属性值
        TypeCost cost = new TypeCost()
                .setType(typeCost.getType())
                .setMessage(typeCost.getMessage());

        //TypeCostMapper.insert(cost)方法插入类型费用的数据,并获取返回结果
        Integer rows = typeCostMapper.insert(cost);
        //判断返回值是否不为1
        if (rows != 1) {
            //是:抛出InsertException异常
            throw new InsertException("添加类型费用失败,请稍后再试!");
        }

        //返回
        return cost;
    }

    @Override
    public TypeCost delete(Integer id, Integer userId) {
        //判断用户id是否是管理员
//        User user = UserMapper.selectById(userId);
//        if(user == null){
//            throw new PermissionDeniedException("您不具备添加类型费用的权限!请登录再试");
//        }

        //根据参数id调用typeCostMapper.selectById(id)查询被删除的"商品类型"的信息
        TypeCost cost = typeCostMapper.selectById(id);
        //判断查询结果是否为null
        if (cost == null) {
            //是:该"商品类型"不存在,抛出StallNotFoundException异常
            throw new StallNotFoundException("删除商品类型失败,尝试访问的商品类型不存在!");
        }

        //根据参数id调用typeCostMapper.deleteById()执行删除,并获取返回的受影响的行数
        Integer rows = typeCostMapper.deleteById(id);
        //判断返回值是否不为1
        if (rows != 1){
            //是:抛出DeleteException
            throw new DeleteException("删除商品类型失败,请稍后再进行尝试");
        }
        //返回查询结果
        return cost;
    }

    @Override
    public TypeCost update(TypeCost typeCost, Integer id, Integer userId) {
        //判断userId是否是管理员
//        User user = UserMapper.selectById(userId);
//        if(user == null){
//            throw new PermissionDeniedException("您不具备添加地摊区域的权限!请登录再试");
//        }

        //根据参数id调用typeCostMapper.selectById(id)查询被删除的"商品类型"的信息
        TypeCost cost = typeCostMapper.selectById(id);
        //判断查询结果是否为null
        if (cost == null) {
            //是:该"商品类型"不存在,抛出StallNotFoundException异常
            throw new StallNotFoundException("修改商品类型失败,尝试访问的商品类型不存在!");
        }

        //创建TypeCost对象
        //补全TypeCost对象的属性值
        cost = new TypeCost()
                .setId(id)
                .setType(typeCost.getType())
                .setMessage(typeCost.getMessage());

        // 根据area调用areaMapper.updateById()执行修改，并获取返回的受影响行数
        Integer rows = typeCostMapper.updateById(cost);
        // 判断返回值是否不为1
        if(rows!=1){
            // 是：抛出UpdateException
            throw new UpdateException("修改失败！服务器繁忙请稍后再试！");
        }

        return cost;
    }
}
